package ca.concordia.soen6011.impl;

import java.util.Date;

import ca.concordia.soen6011.commons.Helpers;
import ca.concordia.soen6011.interfaces.MathFunction;
import ca.concordia.soen6011.Result;
/**
 * Cos (x) transient function
 * -----------------------------------------------------
 * 
 * Calculates the Cos(x)
 * 
 * @author Zhang, Dan
 * @since July-August 2014
 * 
 *        *****************************************************************
 *        TRANSIENT FUNCTION CALCULATOR is a team project for the SOEN 6011
 *        course delivered by Dr. Kamthan, Pankaj at Concordia University,
 *        Montreal for the summer 2014 session.
 *        *****************************************************************
 */
public class Cos implements MathFunction {

	@Override
	public Result compute(double x, double precision) {

      while (x <0 ) x= x+360;		
		x= x% 360;
		Date start = new Date();
		double cosx =1.0000;
		int Coefficient=-1;
		double radX=Helpers.degree2radians(x);
		double pow=0;
		double fac=0;
//		double precisionDouble=1.0;
//		for(int i=0;i<precision;i++)
//			precisionDouble*=0.1;
		double preci=1000;
		for (int n=2; preci > precision;n=n+2){
			 pow=Helpers.power(radX,n);
			fac=Helpers.factorial(n);
			preci=pow/fac;
			cosx += Coefficient*preci; 
			Coefficient*=-1;
		}
		Date end = new Date();

		Result result = new Result();
		result.setComputationTime(end.getTime()-start.getTime());
		result.setOutput(cosx);
		result.setPrecison(precision);
		return result;
	}
		
	
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "Cos(x)";
	}

	@Override
	public void validate(double x, double precision) throws Exception {
		// TODO Auto-generated method stub
		
	} 
		



}
